﻿using System.Data.Services;

namespace AstoriaPlus
{
	public class EntityChangeRules<TDataService, TDataSource> : IEntityChangeRules<TDataService, TDataSource>
		where TDataService : DataService<TDataSource>
		where TDataSource : class
	{
		private readonly ExtendedDataServiceConfiguration<TDataService, TDataSource> configuration;

		public EntityChangeRules(ExtendedDataServiceConfiguration<TDataService, TDataSource> configuration)
		{
			this.configuration = configuration;
		}

		public ExtendedDataServiceConfiguration<TDataService, TDataSource> Configuration
		{
			get { return this.configuration; }
		}

		public IEntityChangeRules<TDataService, TDataSource> InterceptWith(IEntityInterceptorFactory<TDataService, TDataSource> interceptorFactory)
		{
			this.configuration.ChangeInterceptors.Add(interceptorFactory);
			return this;
		}

		public IEntityChangeRules<TDataService, TDataSource> InterceptWith<TEntityInterceptor>()
			where TEntityInterceptor : IEntityInterceptor<TDataService, TDataSource>, new()
		{
			return this.InterceptWith(new DefaultEntityInterceptorFactory<TDataService, TDataSource, TEntityInterceptor>());
		}
	}

	public class EntityChangeRules<TDataService, TDataSource, TEntity> : IEntityChangeRules<TDataService, TDataSource, TEntity>
		where TDataService : DataService<TDataSource>
		where TDataSource : class
		where TEntity : class
	{
		private readonly ExtendedDataServiceConfiguration<TDataService, TDataSource> configuration;
		private readonly string entitySetName;

		public EntityChangeRules(ExtendedDataServiceConfiguration<TDataService, TDataSource> configuration, string entitySetName)
		{
			this.configuration = configuration;
			this.entitySetName = entitySetName;
		}

		public ExtendedDataServiceConfiguration<TDataService, TDataSource> Configuration
		{
			get { return this.configuration; }
		}

		public IEntityChangeRules<TDataService, TDataSource, TEntity> InterceptWith(IEntityInterceptorFactory<TDataService, TDataSource, TEntity> interceptorFactory)
		{
			this.configuration.ChangeInterceptors.Add(interceptorFactory);
			return this;
		}

		public IEntityChangeRules<TDataService, TDataSource, TEntity> InterceptWith<TEntityInterceptor>()
			where TEntityInterceptor : IEntityInterceptor<TDataService, TDataSource, TEntity>, new()
		{
			this.configuration.ChangeInterceptors.Add(new DefaultEntityInterceptorFactory<TDataService, TDataSource, TEntity, TEntityInterceptor>());
			return this;
		}
	}
}
